define(function(require, exports){
	var $ = require("jquery");
	var pubjs   = require('pubjs');
	var view    = require('@base/view');
	var util    = require('util');
	// var adsBox    = require('ads/adsBox');

	// 广告位监测-媒体盒子
	var MediaBox = view.container.extend({
		init: function(config){
			config = pubjs.conf(config,{
				'closeBtn': null,
				'urlSaveMedia': 'massMedia/add',
				'urlSelectMedia': 'massMedia/list',
				'view_model': {
					// 新建渠道名字
					'mediaName': '',
					'showAddMedia': false,
					'eventToggleMedia': this.eventToggleMedia,
					'eventSaveMedia': this.eventSaveMedia,
					'eventHideAddMedia': this.eventHideAddMedia
				}
			});
			this.$adsIds = 0;
			this.$adsLists = [];
			this.Super('init',arguments);
		},
		afterBuild: function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			$([
				'<div class="M-formConGroup">',
					'<label class="group-label">'+LANG('媒体：')+'</label>',
					'<div class="group-content">',
						'<span pub-mod="@base/common/drop.drop" pub-name="media"/>',
						'<span class="ml10" pub-name="mediaTip" pub-mod="@base/tip.desc"/>',
						'<span class="ml20 btn btn-default" ms-click="eventToggleMedia">' + LANG('新建媒体') + '</span>',
						'<div class="mt30" ms-visible="showAddMedia">',
							'<label class="mr10">' + LANG('媒体名字') + '</label>',
							'<span>',
								'<input ms-duplex-value="mediaName"/>',
							'</span>',
							'<button class="btn btn-confirm ml15" ms-click="eventSaveMedia">'+ LANG('保存') + '</button>',
							'<button class="btn btn-default ml15" ms-click="eventHideAddMedia">'+ LANG('取消') + '</button>',
						'</div>',
					'</div>',
				'</div>',

				'<div>',
					'<i class="close icon-close mediaCloseBtn"></i>',
				'</div>',

				'<div class="P-adsAddCon" pub-name="adsBox_0" pub-mod="ads/adsBox.main"></div>',

				'<div class="P-adsAddCon tc addBtnBox">',
					'<button class="adsAddBtn addBtn btn btn-default">',
						'<i class="uk-icon-plus"></i>',
						LANG('继续添加广告位'),
					'</button>',
				'</div>'

			].join('')).appendTo(el);

			//绑定添加广告位按钮
			self.uiBind(el.find('.adsAddBtn'), 'click', self.eventAddAds);

			//绑定媒体框的关闭按钮
			if(!c.closeBtn){
				self.find('.mediaCloseBtn').hide();
			}
			else{
				self.uiBind(el.find('.mediaCloseBtn'), 'click', self.eventCloseBtn);
			}

			var config = {
				'media': {
					'placeholder': LANG('新建媒体'),
					'type': 'button',
					'def': '请选择媒体',
					'width': 250,
					'panel_config': {
						'height': 200,
						'hasSearch': true
					}
				},
				'mediaTip':{
					'data': LANG('标记广告位所用，譬如“凤凰网”可以算是一个媒体。不影响广告投放。')
				}
			};
			self.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
			var self = this;
			// 记录adsBox零号机
			self.$adsLists[self.$adsIds] = self.$.adsBox_0;
			self.$adsIds++;
		},
		//添加广告位
		eventAddAds: function(){
			var self = this;
			var el = self.getDOM();

			//点击构建新的广告位
			self.createAsync(
				'adsBox_'+self.$adsIds,
				'ads/adsBox.main',
				{
					'target': el,
					'class':'P-adsAddCon',
					'default_hide': true,
					'closeBtn': self.$adsIds > 0 // 是否有关闭按钮
				},
				function(mod){
					self.$adsLists[self.$adsIds] = mod;
					el.find('.addBtnBox').appendTo(el);
					mod.show(100);
				}
			);

			self.$adsIds++;
			return false;
		},
		eventCloseBtn: function(ev){
			var self = this;
			//清除模块
			self.fire('mediaCloseBtn', ev);
			self.destroy(true);
			return false;
		},
		// 媒体更改，同步更改广告位的名称
		filterAddName: function(){
			var self = this;
			util.each(self.$adsLists, function(mod){
				if(mod){
					mod.filterAddName();
				}
			});
		},
		onDropChange: function(ev){
			this.filterAddName();
			return false;
		},
		onDropBlur: function(ev){
			this.filterAddName();
			return false;
		},
		onAdsCloseBtn: function(ev){
			var tmpId = ev.name.split('_')[1];
			this.$adsLists[tmpId] = null;

			return false;
		},
		//提取数据为  [object,..]
		getData: function(){
			var self = this;
			var data = [];
			var tmp = {};
			for(var i =0;i<self.$adsLists.length;i++){
				if(self.$adsLists[i]){
					tmp = self.$adsLists[i].getData();
					tmp.MediaName = self.$.media.getValue(true).name;
					tmp.MassMediaId = self.$.media.getValue(true).id;
					data.push(tmp);
				}
			}
			return data;
		},
		setData: function(data){
			var self =this,
				y = 0;
			self.$.media.setValue(data[0].MassMediaId);
			for(var i=0;i<data.length && y<self.$adsLists.length;){
				if(self.$adsLists[y]){
					self.$adsLists[y].setData(data[i]);
					i++;
					y++;
				}
				y++;
			}
		},
		reset: function(){
			var self = this;
			self.$data = [];
			self.$.media.setValue('');
			for(var i=1;i<self.$adsLists.length;i++){
				if(self.$adsLists[i]){
					self.$adsLists[i].destroy();
					self.$adsLists[i] = null;
				}
			}
			self.vm.$.showAddMedia = false;
			self.$adsLists[0].reset();
			self.$adsLists.splice(1,self.$adsLists.length-1);
		},
		eventToggleMedia: function()
		{
			var vm = this.vm.$;
			vm.showAddMedia = !vm.showAddMedia;
		},
		eventSaveMedia: function()
		{
			var self = this;
			var vm = self.vm.$;
			var name = vm.mediaName;
			var ChannelId = +self.$ChannelId;
			var url = self.getConfig('urlSaveMedia');

			if (!name)
			{
				pubjs.alert(LANG('请输入媒体名称'));
				return false;
			}

			if (!ChannelId)
			{
				pubjs.alert(LANG('请选择渠道'));
				return false;
			}

			pubjs.mc.send(url, {
					'Name': name,
					'AdChannelId': 2,
					'AdSubChannelId': ChannelId
				}, function(err, data){
					if (err)
					{
						pubjs.alert(err.message);
						return false;
					}
					self.fire('upateMediaData');
					// 隐藏添加媒体模块
					self.eventHideAddMedia();
					// 更新渠道下拉菜单的数据
					// self.$.channel.load();
			});
		},
		eventHideAddMedia: function()
		{
			var vm = this.vm.$;
			vm.showAddMedia = false;
			vm.mediaName = '';
		},
		setChannelData: function(channelId)
		{
			this.$ChannelId = channelId;
		}
	});
	exports.main = MediaBox;
});